# Base controller class for all controllers
# This provides common functionality and patterns for controller layer classes

from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional
import logging
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse

class BaseController(ABC):
    """
    Abstract base class for all controller layer classes.
    Provides common functionality and enforces controller patterns.
    """
    
    def __init__(self):
        self.logger = logging.getLogger(self.__class__.__name__)
        self.router = APIRouter()
    
    def log_info(self, message: str, **kwargs):
        """Log info message with optional context"""
        self.logger.info(message, extra=kwargs)
    
    def log_error(self, message: str, exception: Exception = None, **kwargs):
        """Log error message with optional exception and context"""
        if exception:
            self.logger.error(f"{message}: {str(exception)}", extra=kwargs)
        else:
            self.logger.error(message, extra=kwargs)
    
    def log_warning(self, message: str, **kwargs):
        """Log warning message with optional context"""
        self.logger.warning(message, extra=kwargs)
    
    def success_response(self, data: Any = None, message: str = "Success") -> JSONResponse:
        """Return a standardized success response"""
        return JSONResponse(
            status_code=200,
            content={
                "success": True,
                "message": message,
                "data": data
            }
        )
    
    def error_response(self, message: str, status_code: int = 400, error_code: str = None) -> HTTPException:
        """Return a standardized error response"""
        detail = {
            "success": False,
            "message": message
        }
        if error_code:
            detail["error_code"] = error_code
        
        raise HTTPException(status_code=status_code, detail=detail)